// 端口号3010
// const { send } = require('express/lib/response')
const { send } = require('express/lib/response')
const dbConfig = require('../config/dbConfig')
const { log } = console
module.exports = {

  // pc端管理员----------------------------------------------
  login(req, res) {
    let { admin_account, admin_password } = req.body
    log('参数打印:' + admin_account, admin_password)
    const sql = "select admin_id,admin_account,admin_iphone from admin where admin_account = ? and admin_password = ?"
    const arr = [admin_account, admin_password]
    dbConfig.db(res, sql, arr, function (data) {
      res.send({
        code: 200,
        msg: '查询成功',
        data: data
      })
    })
  },// 管理员账户信息
  getAdminData(req,res){
    let sql = "select admin_id,admin_iphone from admin"
    const arr =[]
    dbConfig.db(res,sql,arr,function (data) {
      res.send({
        code:200,
        msg:'查询成功',
        data
      })
    })
  },
  updateAdminIphone(req, res) {
    let { admin_id, admin_iphone } = req.body
    log('参数打印:' + admin_id, admin_iphone)
    const sql = "UPDATE admin SET admin_iphone = ? WHERE admin_id = ?"
    const arr = [admin_iphone, admin_id]
    dbConfig.db(res, sql, arr, function (data) {
      log(data)
      if (data.changedRows > 0) {
        res.send({
          code: 200,
          msg: '修改成功',
        })
      }
    })
  },//修改管理员电话

  // pc端管理员结束==========================================


  // pc端用户管理页面---------------------------------
  getUserData(req, res) {
    const sql1 = "select * from user"
    const arr1 = []
    const date = new Date()
    let year = date.getFullYear()
    let month = '' + date.getMonth()
    let day = '' + date.getDate()
    if (month.length < 2) month = '0' + month
    if (day.length < 2) day = '0' + day
    const newDate = year + '-' + month + '-' + day
    let sql2 = 'SELECT count(*) AS newAddUser FROM user WHERE ? < user_dateTime'
    const arr2 = [newDate]

    const newAddUser = () => {
      return new Promise((resolve, reject) => {
        dbConfig.db(res, sql2, arr2, function (data2) {
          resolve(data2)
        })
      })
    }

    newAddUser()
      .then((data2) => {
        dbConfig.db(res, sql1, arr1, function (data1) {
          res.send({
            code: 200,
            msg: '查询成功',
            data: {
              data: data1,
              newAddUser: data2[0].newAddUser,
            }
          })
        })
      })
  },//获取用户信息
  getSearchData(req, res) {
    const { user_id, user_name, user_iphone } = req.query
    log('参数打印:' + user_id, user_name, user_iphone)
    let sql = "select * from user where 1=1"
    const arr = []
    if (user_id.length > 0) {
      sql += ' and user_id= ? '
      arr.push(user_id)
    }
    if (user_name.length > 0) {
      sql += ' and user_name= ? '
      arr.push(user_name)
    }
    if (user_iphone.length > 0) {
      sql += ' and user_iphone= ? '
      arr.push(user_iphone)
    }
    dbConfig.db(res, sql, arr, function (data) {
      if (data.length > 0) {
        res.send({
          code: 200,
          msg: '查询成功',
          data: data
        })
      } else {
        res.send({
          code: 200,
          msg: '没有此用户'
        })
      }
    })
  },//用户搜索
  // pc端用户管理页面结束==========================================


  // pc端用户卡卷----------------------------------------------
  getCouponData(req, res) {
    const { user_id } = req.query
    log('参数打印:' + user_id)
    let sql = "SELECT * FROM coupon JOIN couponhistory ON couponhistory_coupon_id = coupon_id JOIN user ON user_id = couponhistory_user_id WHERE user_id = ?"
    const arr = [user_id]
    dbConfig.db(res, sql, arr, function (data) {
      res.send({
        code: 200,
        msg: '查询成功',
        data: data
      })
    })
  },
  // pc端用户卡卷结束================================================

  // 图片上传
  uploadPicture(req, res) {
    const picture_src = req.file.filename
    log('参数打印:' + picture_src)
    let sql1 = " insert into picture(picture_src) values (?)"
    let arr1 = [picture_src]
    let sql2 = "select * from picture where picture_src = ?"
    const fn1 = () => {
      return new Promise((resolve, reject) => {
        dbConfig.db(res, sql1, arr1, function (data) {
          if (data.affectedRows > 0) {
            resolve()
          } else {
            reject(res)
          }
        })
      })
    }

    fn1()
      .then(() => {
        dbConfig.db(res, sql2, arr1, function (data) {
          res.send({
            code: 200,
            msg: '上传成功',
            data: data
          })
        })
      })
      .catch((error) => {
        error.send({
          code: 200,
          msg: '上传失败'
        })
      })
  },
  getUploadData(req, res) {
    let sql = 'select * from picture'
    const arr = []
    dbConfig.db(res, sql, arr, function (data) {
      if (data.length > 0) {
        res.send({
          code: 200,
          msg: '查询成功',
          data: data
        })
      }
    })
  },//获取图片
  setUploadData(req, res) {
    let { picture_id, picture_src } = req.query
    let sql = 'update picture set picture_src = ? where picture_id = ?'
    const arr = []
    dbConfig.db(res, sql, arr, function (data) {
      log(data)
    })
  },//设置图片
  // 图片结束================================================

  // pc卡卷管理---------------------------------
  //获取卡劵信息
  getCoupon(req, res) { 
    const sql = "SELECT * FROM coupon where coupon_delete = 1"
    const arr = []
    dbConfig.db(res, sql, arr, function (data) {
      res.send({
        code: 200,
        msg: '查询成功',
        data
      })
    })
  },
  // 修改卡劵名称和卡劵类型
  updateCouponData(req, res) {
    const { coupon_name, coupon_money, coupon_id } = req.body
    log('参数打印:' + coupon_name, coupon_money, coupon_id)
    let sql = 'update coupon set'
    const arr = []
    if (coupon_name != undefined) {
      sql = sql + " coupon_name = ?"
      arr.push(coupon_name)
    }
    if (coupon_money != undefined) {
      coupon_name == undefined ? sql = sql + ' coupon_money = ?' : sql = sql + ',coupon_money = ?'
      arr.push(coupon_money)
    }
    arr.push(coupon_id)
    sql = sql + ' WHERE coupon_id = ?'
    dbConfig.db(res, sql, arr, function (data) {
      res.send({
        code: 200,
        msg: '更新成功',
        data
      })
    })
  },
  // 修改状态
  updateCouponState(req, res) {
    let { coupon_id, coupon_state } = req.body
    coupon_state == 1 ? coupon_state = 0 : coupon_state = 1
    log('参数打印:' + coupon_state,coupon_id)
    let sql = 'update coupon set coupon_state = ? where coupon_id = ?'
    const arr = [coupon_state,coupon_id]
    dbConfig.db(res,sql,arr,function(data)  {
      res.send({
        code:200,
        msg:'修改成功',
        data
      })
    })
  },
  // 删除卡劵
  deleteCouponRow(req,res) {
    let { coupon_id } = req.body
    log('参数打印:',coupon_id)
    let sql = "update coupon set coupon_delete = 0 where coupon_id = ?"
    const arr = [coupon_id]
    dbConfig.db(res,sql,arr,function(data) {
      res.send({
        code:200,
        msg:'删除成功',
        data
      })
    })
  },
  //添加-卡劵
  addCoupon(req,res){
    const {coupon_name,coupon_money,coupon_state,coupon_type} = req.body
    const sql = 'INSERT INTO coupon(coupon_name,coupon_money,coupon_state,coupon_type,coupon_delete) VALUES (?,?,?,?,1)'
    const arr = [coupon_name,coupon_money,coupon_state,coupon_type]
    dbConfig.db(res,sql,arr,function(data) {
      res.send({
        code:200,
        msg:'添加成功',
        data
      })
    })
  },

// pc卡卷管理结束====================================
// -------------------学时排行-------------
 // 用户时间
 getUserTime (req, res) {
  //  where 开始时间  《 user_dateTime 《 结束时间
  const sql = 'select * from user order by user_studyTime desc'
  const arr = []
  dbConfig.db(res, sql, arr, function (data) {
    res.send({
      code: 200,
      msg: '查询成功',
      data
    })
  }
  )
},
// const sql = SELECT * FROM USER WHERE user_dateTime  BETWEEN ?   AND   ?

getDatequery (req, res) {
  //  where 开始时间  《 user_dateTime 《 结束时间
  const { user_statrTime, user_endTime } = req.query
  log(user_statrTime, user_endTime)
  const sql = 'SELECT * FROM USER WHERE user_dateTime  BETWEEN ? AND  ?'
  const arr = [user_statrTime, user_endTime]
  dbConfig.db(res, sql, arr, function (data) {
    res.send({
      code: 200,
      msg: '查询成功',
      data
    })
  }
  )
},
//-------------座位管理
    // 获取座位数据
    getSeatdetailsData(req, res) {
      let sql = 'select * from seat'
      const arr = []
      dbConfig.db(res, sql, arr, function(data) {
          if (data.length > 0) {
              res.send({
                  code: 200,
                  msg: '查询成功',
                  data: data
              })
          }
      })
  },
  updateSeat(req, res) {
      const { seat_power, seat_id } = req.body
      console.log('传递数组');
      console.log(seat_id);
      log('参数打印：', seat_power, seat_id)
      const sql = "UPDATE seat SET seat_power = ? WHERE seat_id in (?)"
      const arr = [seat_power, seat_id]
      dbConfig.db(res, sql, arr, function(data) {
          res.send({
              code: 200,
              msg: '成功',
              data
          })
      })
  },
   // 充值管理
   ctyGetadmin(req, res) {

    let sql = 'select * from money_management '
    const arr = []
    dbConfig.db(res, sql, arr, function(data) {
        res.send({
            code: 200,
            data: data
        })
    })

},
// 移动端充值
addmoney(req, res) {
    let { money_id, phones } = req.body
    const sql = 'insert into user_money (user_phone,money_id) values (? ,?)'
    const arr = [phones, money_id]
    dbConfig.db(res, sql, arr, function(data) {
        res.send({
            code: 200,
            data: data
        })
    })
},

// **************************************
  // 手机端:
  // 卡劵商场
  getCouponStore(req,res){
    let sql = "select * from coupon where coupon_state = 1 and coupon_delete = 1"
    const arr = []
    dbConfig.db(res,sql,arr,function(data) {
      res.send({
        code:200,
        msg:'查询成功',
        data
      })
    })
  },
  applogin(req, res) {
    let code = req.body.code
    console.log(code, 'login');
    res.send(code)
  },
    // app用户登录
    apprelogin(req,res){
      let id=req.body.id
      let sql = 'select * from user where user_opid=?'
      const arr = [id]
      dbConfig.db(res, sql, arr, function (data) {
          res.send({
            code: 200,
            msg: '查询成功',
            data: data
          })
      })
    }, 
     // app添加用户
    appadduser(req, res) {
      let { name, opid } = req.body;
      // 模拟手机号
      let pharea = [3, 4, 5, 7, 8];
      let randarea = Math.floor(Math.random() * (0 - 5) + 5);
      let num = '';
      for (let i = 0; i < 9; i++) {
        let randnum = String(Math.floor(Math.random() * (0 - 10) + 10));
        num += randnum
      }
      let phone = '1' + pharea[randarea] + num;
      // 注册时间
      let date = new Date()
      let year = date.getFullYear()< 10 ? "0" + date.getFullYear() : date.getFullYear();
      let month = (date.getMonth() + 1)< 10 ? "0" + (date.getMonth()+1) : (date.getMonth()+1);
      let daty = date.getDate()< 10 ? "0" + date.getDate() : date.getDate();
      let h = date.getHours()< 10 ? "0" + date.getHours() : date.getHours();
      let s = date.getMinutes()< 10 ? "0" + date.getMinutes() : date.getMinutes();
      let m = date.getSeconds()< 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 注册时间
      let user_dateTime=year + '-' + month+ '-' + daty + ' ' + h + ':' + s + ':' + m
      console.log(user_dateTime);
      // 钱包余额
      let user_money=0;
      // 兑换时长
      
      let user_useTime=0;
      // 学习时间
      let user_studyTime=0;
      let arr = [name, opid, phone,user_studyTime,user_money,user_useTime,user_dateTime];
      let sql = `insert into user(user_name,user_opid,user_iphone,user_studyTime,user_money,user_useTime,user_dateTime) values (?,?,?,?,?,?,?)`;
      dbConfig.db(res, sql, arr, function (data) {
        if (data.affectedRows === 1) {
          res.send({
            code: 200,
            msg: '添加用户成功'
          })
        }
      })
    },
    // 打卡
    appcardtime(req,res){
      let {Time,opid}=req.body
      let sql = 'insert into hitcart(hitcart_dateTime,hitcart_user_id) values(?,?)'
      const arr = [Time,opid]
      dbConfig.db(res, sql, arr, function (data) {
          res.send({
            code: 200,
            msg: '打卡成功',
            data: data
          })
      })
    },
    // 获取打卡数据
    appGetcardTime(req,res){
      let id=req.body.id
      let sql='select * from hitcart where hitcart_user_id=?'
      let arr=[id];
      dbConfig.db(res, sql, arr, function (data) {
        res.send({
          code: 200,
          msg: '查询成功',
          time:new Date().toLocaleDateString(),
          data: data,
          info:'打卡',
        })
    })
    }

}